package com.whoami.dataplatform.tag_combine.hbase.rw;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.BitSet;
import java.util.List;

/**
 * @author tzp
 * @since 2019/8/12
 */
public class TableRead {
    private static final String TABLE_NAME = "test_java";
    private static final String CF_DEFAULT = "cf1";

    public static void main(String[] args) throws IOException {
        Configuration config = HBaseConfiguration.create();
        config.addResource(new Path(System.getenv("HBASE_CONF_DIR") + "/conf", "hbase-site.xml"));
        scanTU(config);
    }

    public static void get(Configuration config) throws IOException {
        try (Connection connection = ConnectionFactory.createConnection(config)) {
            TableName tableName = TableName.valueOf(TABLE_NAME);
            Table table = connection.getTable(tableName);

            Get get = new Get("row1".getBytes());
            get.setMaxVersions(100)
                    .addFamily(CF_DEFAULT.getBytes());
            Result result = table.get(get);

//            byte[] value = result.getValue(CF_DEFAULT.getBytes(), null);
//            System.out.println(new String(value));

//            CellScanner cellScanner = result.cellScanner();
//            Cursor cursor = result.getCursor();
            List<Cell> columnCells = result.getColumnCells(CF_DEFAULT.getBytes(), "c1".getBytes());
            for (Cell c : columnCells) {
                printCell(c);
            }
            System.out.println("-------------");

            for (Cell c : result.rawCells()) {
                printCell(c);
            }

        }
    }

    public static void printCell(Cell c) {
        System.out.println(Bytes.toString(CellUtil.cloneRow(c))
                + "==> " + Bytes.toString(CellUtil.cloneFamily(c))
                + "{" + Bytes.toString(CellUtil.cloneQualifier(c))
                + ":" + Bytes.toString(CellUtil.cloneValue(c)) + "}");
    }

    public static void scanTU(Configuration config) throws IOException {
        try (Connection connection = ConnectionFactory.createConnection(config)) {
            TableName tableName = TableName.valueOf("test_tu");
            Table table = connection.getTable(tableName);
//            69.4.3
            Scan scan = new Scan();
            scan.addColumn(CF_DEFAULT.getBytes(), null);
            ResultScanner rs = table.getScanner(scan);
            for (Result r = rs.next(); r != null; r = rs.next()) {
                for (Cell c : r.listCells()) {
                    BitSet bitSet = BitSet.valueOf(CellUtil.cloneRow(c));
                    int i = Bytes.toInt(CellUtil.cloneValue(c));
                    System.out.println(bitSet.toString() + " " + i);
                }
            }
        }
    }


}

